package org.whb.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whb.basic.util.AjaxResult;
import org.whb.basic.util.LoginContext;
import org.whb.basic.util.PageList;
import org.whb.org.service.IEmployeeService;
import org.whb.pet.domain.Pet;
import org.whb.pet.domain.SearchMasterMsg;
import org.whb.pet.query.SearchMasterMsgQuery;
import org.whb.pet.service.ISearchMasterMsgService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;
    @Autowired
    private IEmployeeService employeeService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getLogininfo(request));
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后再试！！！");
        }
    }

    /**
     * 寻主待处理信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.pending(query,LoginContext.getLogininfo(request));
    }

    /**
     * 查询已处理的寻主信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.processed(query,LoginContext.getLogininfo(request));
    }

    /**
     * 处理寻主消息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getLogininfo(request));
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后再试！！！");
        }
    }

}
